<?php
namespace app\admin\controller;

use think\Loader;
use think\queue\connector\Redis;
use think\Request;
use app\common\controller\Base;
use app\common\service\ProductCat as ProductCatService;
use app\common\model\ProductCat as ProductCatModel;

class ProductCat extends Base{

    /**
     * 产品分类列表
     */
    public function index(){

        $productCat = ProductCatModel::where('pid',0)->whereOr('pid','null')->select();

        return view('',['productCat'=>$productCat]);
    }

    public function add(){

        return view();
    }

    public function create(Request $request){

        if(!$request->isPost()){
            $this->error("非法途径访问");
        }

        $data = $request->only(['name','catway','cover','desc']);

        $productCatService = new ProductCatService();
        $result = $productCatService ->insert($data);

        if($result['state']) {
            $this->success("操作成功");
        }else{
            $this->error($result['message']);
        }


    }


    public function edit(Request $request){
        $id = $request ->param('id');
        $productCatService = new ProductCatService();
        $productCat = $productCatService->find($id);
        return view('',['productCat'=>$productCat]);
    }

    public function update(Request $request){

        if(!$request->isPost()){
            $this->error("非法途径访问");
        }

        $data = $request->only(['name','catway','cover','desc']);
        $id = $request->param('id',0,'intval');
        if(!$id){
            $this->error("非法途径访问");
        }

        $validate = Loader::validate('ProductCatValidate');
        $result = $validate-> scene('edit')->check($data);
        if(!$result){
            $this->error($validate->getError());
        }

        $productCatModel = ProductCatModel::find($id);
        $productCatModel->save($data);

        $this->success("操作成功");

    }


    public function delete(Request $request){

        if(!$request->isDelete()){
            $this->error("非法访问途径");
        }
        $id = $request->param('id');
        $productCatService = new ProductCatService();
        $count = $productCatService -> delete($id);

        if($count){
            $this->success("操作成功");
        }else{
            $this->error("操作失败");
        }
    }




    //系统分类的子类管理
    public function subIndex(Request $request){
        $id = $request->param('id',0,'intval');
        if(!$id){
            $this->error("非法访问途径");
        }
        $productCat = ProductCatModel::find($id);
        $productCats = ProductCatModel::where("pid",$id)->select();

        return view('',['productCat'=>$productCat,'productCats'=>$productCats]);
    }

    public function subAdd(Request $request){
        $id = $request->param('id',0,'intval');
        if(!$id){
            $this->error("非法访问途径");
        }

        $parentCat = ProductCatModel::find($id);


        return view('',['parentCat'=>$parentCat]);
    }


    public function subCreate(Request $request){
        if(!$request->isPost()){
            $this->error("非法访问途径");
        }
        $data = $request -> only(['name','pid','catway','cover','desc']);

        $validate = Loader::validate('ProductCatValidate');
        $result = $validate-> scene('add')->check($data);
        if(!$result){
            $this->error($validate->getError());
        }

        $productCatModel = new ProductCatModel();
        $productCatModel -> save($data);

        $this->success("添加成功");
    }

    public function subEdit(Request $request){
        $id = $request -> param('id',0,'intval');
        if(!$id){
            $this->error("非法访问渠道");
        }

        $productCat = ProductCatModel::find($id);

        return view('',['productCat'=>$productCat]);

    }


    public function subUpdate(Request $request){
        if(!$request->isPost()){
            $this->error("非法访问渠道");
        }

        $data = $request -> only(['name','pid','catway','cover','desc']);
        $id = $request -> param('id',0,'intval');
        if(!$id){
            $this->error("非法访问渠道");
        }
        $validate = Loader::validate('ProductCatValidate');
        $result = $validate-> scene('add')->check($data);
        if(!$result){
            $this->error($validate->getError());
        }

        $productCatModel = ProductCatModel::find($id);
        $productCatModel->save($data);

        $this->success("更新成功");

    }


    public function seo(Request $request){
        $id = $request->param("id",0,'intval');
        $productCat = ProductCatModel::find($id);

        return view('',['post'=>$productCat]);
    }


    public function seoUpdate(Request $request){
        if(!$request->isPost()){
            $this->error('非法访问渠道');
        }

        $data = $request->only(['seo_title','seo_keywords','seo_description']);
        $id = $request -> param('id',0,'intval');
        if(!$id){
            $this->error("非法访问渠道");
        }
        $productCatModel = ProductCatModel::find($id);
        $productCatModel -> save($data);

        $this->success("更新成功");
    }


}
